// pages/outvisit/taskList/taskList.js
var api = require("../../../utils/api.js");
var util = require("../../../utils/util.js");
var common = require("../../../utils/common.js");
var pagetitle = ["","预约任务", "我的任务", "已完成任务"];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0,//任务类型
    totalPage: 0,//总页数
    nowPage: 1,//当前页码
    pagesize: 10,//页大小
    taskList: [],//任务列表
    outvisitId: 0,//当前的任务id
    delayFormIsHide: true,//延时外访表单是否隐藏
    returnFormIsHide: true,
    noDataIsHide: true,//是否隐藏“没有数据”
    pickerValueReturnType: [],
    returnTypeList: common.returnTypeList,//信箱情况列表
    addressTypeList: common.addressTypeList,//地址类型列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    // 设置标题
    wx.setNavigationBarTitle({
      title: pagetitle[type]
    });
    // 设置任务类型
    that.setData({
      type: type
    },() => {
      // 获取任务列表
      that.getAppointmentList();
    });
  },

  // 录入信息
  returnInfo:function(e){
    var that = this;
    wx.navigateTo({
      url: 'returnInfo?id=' + e.currentTarget.dataset.id
    });
  },

  // 预约信息
  appointmentInfo:function(e){
    var that = this;
    wx.navigateTo({
      url: 'appointmentInfo?id=' + e.currentTarget.dataset.id
    });
  },

  // 设置表单下拉框默认值
  setPickerValueReturnType: function (data) {
    var that = this;
    var pickerValueReturnType = that.data.pickerValueReturnType;
    if (data.returnType) {
      var returnTypeList = that.data.returnTypeList;
      for (var i = 0; i < returnTypeList.length; i++) {
        if (data.returnType == returnTypeList[i].id) {
          pickerValueReturnType[0] = i;
        }
      }
      // 获取外访场景列表
      wx.showLoading({
        title: '加载中',
      });//显示加载中
      api.getReturnTypeList(that.data.returnTypeList[pickerValue[0]].id).then(res => {
        if (res.code == 0) {
          wx.hideLoading();//隐藏加载中
          var returnTypeList = res.data;

          if (data.rcode) {
            for (var i = 0; i < rcodeList.length; i++) {
              if (data.rcode == rcodeList[i].id) {
                pickerValuePuFa[1] = i;
              }
            }
          }
          that.setData({
            pickerValuePuFa: pickerValuePuFa,
            returnTypeList: returnTypeList
          });
        }
      });
    }
  },

  //下拉框选择事件
  bindPickerChange: function (e) {
    let pickerValue = this.data.pickerValue;
    pickerValue[parseInt(e.currentTarget.dataset.id)] = e.detail.value;
    this.setData({
      pickerValue: pickerValue
    });
  },

  // 获取任务列表
  getAppointmentList:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    });//显示加载中
    api.getAppointmentList(that.data.nowPage, that.data.pagesize).then(res => {
      console.log(res);
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        var dataList = res.data.data;
        if (dataList && dataList.length>0){
          var taskList = [];
          if (that.data.nowPage!=1){
            taskList = that.data.data;
          }
          for (var i = 0; i < dataList.length; i++) {
            var data = dataList[i];
            data.delay = false;
            taskList.push(data);
          }
          that.setData({
            totalPage: 1,
            taskList: taskList,
            noDataIsHide: true
          });
        }else{
          that.setData({
            taskList: [],
            noDataIsHide:false
          });
        }
      }
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    });
  },

  // 刷新列表
  updateList:function(){
    var that = this;
    that.setData({
      nowPage: 1
    }, res => {
      // 获取任务列表
      that.getTaskList();
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    // 刷新列表
    that.updateList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.nowPage < that.data.totalPage) {
      that.setData({
        nowPage: that.data.nowPage + 1
      }, res => {
        // 获取任务列表
        that.getTaskList();
      });
    }else{
      wx.showToast({
        title: '没有更多数据',
        icon: 'none'
      });
    }
  },

  // 跳转搜索
  searchTo: function (option) {
    wx.navigateTo({
      url: '../../search/search?type=' + this.data.type
    });
  },

  // 延时外访
  delay: function (e) {
    var that = this;
    var cause = e.detail.value.cause;
    if (!cause.trim()) {
      wx.showToast({
        title: "延时原因不能为空",
        icon: "none"
      });
      return;
    }
    wx.showLoading();//显示加载中
    api.delayedOut(that.data.outvisitId, cause).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        that.setData({
          delayFormIsHide: true
        })
        wx.showToast({
          title: "延时成功",
          icon: "none"
        });
        setTimeout(() => {
          // 刷新列表
          that.updateList();
        }, 500);
      }
    });
  },

  // 延时外访
  returnFn: function (e) {
    var that = this;
    var cause = e.detail.value.cause;
    if (!cause.trim()) {
      wx.showToast({
        title: "延时原因不能为空",
        icon: "none"
      });
      return;
    }
    wx.showLoading();//显示加载中
    api.delayedOut(that.data.outvisitId, cause).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        that.setData({
          returnFormIsHide: true
        })
        wx.showToast({
          title: "延时成功",
          icon: "none"
        });
        setTimeout(() => {
          // 刷新列表
          that.updateList();
        }, 500);
      }
    });
  },

  // 延时外访表单显示与隐藏
  delayFormShowOrHide: function (e) {
    var delayFormIsHide = this.data.delayFormIsHide;
    if (delayFormIsHide) {
      delayFormIsHide = false;
      this.setData({
        id: e.currentTarget.dataset.id
      });
    } else {
      delayFormIsHide = true;
    }
    this.setData({
      delayFormIsHide: delayFormIsHide
    })
  },

  // 延时外访表单显示与隐藏
  returnFormShowOrHide: function (e) {
    var returnFormIsHide = this.data.returnFormIsHide;
    if (returnFormIsHide) {
      returnFormIsHide = false;
      this.setData({
        id: e.currentTarget.dataset.id
      });
    } else {
      returnFormIsHide = true;
    }
    this.setData({
      returnFormIsHide: returnFormIsHide
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
